/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path')
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const productionGzipExtensions = ['js', 'css', 'svg', 'eot', 'ttf']
module.exports = {
  publicPath: process.env.NODE_ENV === 'production' ? '/' : '/',
  outputDir: 'dist',
  assetsDir: 'static',
  indexPath: 'index.html',

  // 生成的静态资源在它们的文件名中包含了 hash 以便更好的控制缓存
  filenameHashing: true,

  // 构建时开启多进程处理 babel 编译
  parallel: require('os').cpus().length > 1,

  pages: {
    index: {
      entry: 'src/main.ts',
      template: 'index.html',
      filename: 'index.html',
      title: '顺子个人小站',
      chunks: ['chunk-vendors', 'chunk-common', 'index'],
      cdn:
        process.env.NODE_ENV === 'production'
          ? {
              css: ['https://unpkg.com/view-design@4.2.0/dist/styles/iview.css'],
              js: [
                'https://cdn.jsdelivr.net/npm/vue@2.6.12',
                'https://cdn.bootcss.com/vuex/3.1.3/vuex.min.js',
                'https://cdn.bootcss.com/vue-router/3.1.6/vue-router.js',
                'https://unpkg.com/view-design@4.2.0/dist/iview.min.js'
              ]
            }
          : {}
    }
  },

  productionSourceMap: false,

  configureWebpack: {
    externals:
      process.env.NODE_ENV === 'production'
        ? {
            vue: 'Vue',
            vuex: 'Vuex',
            'vue-router': 'VueRouter',
            'view-design': 'iview',
            iview: 'ViewUI'
          }
        : {},
    devtool: process.env.NODE_ENV === 'production' ? false : 'inline-source-map',
    performance: {
      hints: false,
      maxEntrypointSize: 512000,
      maxAssetSize: 512000
    },
    resolve: {
      extensions: ['.js', '.vue', '.json', 'ts'],
      alias: {
        '@': path.join(__dirname, 'src'),
        api: path.join(__dirname, 'src/api'),
        utils: path.join(__dirname, 'src/utils')
      }
    },
    plugins: [
      // 压缩插件
      new CompressionWebpackPlugin({
        filename: '[path].gz[query]',
        algorithm: 'gzip',
        test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
        threshold: 10240,
        minRatio: 0.8
      })
    ]
  },

  chainWebpack: config => {
    config.module.rule('eslint').exclude.end()
    // 移除 prefetch 插件
    config.plugins.delete('prefetch-index').delete('preload-index')
  },

  css: {
    requireModuleExtension: true,
    extract: process.env.NODE_ENV === 'production' ? true : false,
    sourceMap: process.env.NODE_ENV === 'production' ? false : true,
    loaderOptions: {
      css: {
        modules: {
          localIdentName: '[local]_[hash:base64:8]'
        }
      }
    }
  },

  devServer: {
    open: true,
    // host: 'localhost',
    port: 3008,
    https: false,
    hot: false,
    hotOnly: false,
    compress: true,
    proxy: {
      '/api': {
        target: 'https://apipcs3.dccnet.com.cn', // 接口地址
        changeOrigin: true,
        // ws: true,//websocket支持
        secure: false,
        pathRewrite: {
          '^/api': '/api'
        }
      }
    }
  },
  pluginOptions: {
    'style-resources-loader': {
      preProcessor: 'less',
      patterns: [path.resolve(__dirname, './src/styles/variable.less')]
    }
  }
}
